package com.hq.base.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateUnit;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.io.unit.DataUnit;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.hq.base.entity.ApiLog;
import com.hq.base.mapper.ApiLogMapper;
import com.hq.base.service.ApiLogService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.Comparator;
import java.util.Date;
import java.util.List;
import java.util.Optional;
import java.util.function.ToLongFunction;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 卢杰
 * @since 2021-04-14
 */
@Service
public class ApiLogServiceImpl extends ServiceImpl<ApiLogMapper, ApiLog> implements ApiLogService {

    @Override
    public ApiLog queryLastLog(String api) {
        String nowYMD = DateUtil.format(new Date(), DatePattern.NORM_DATE_PATTERN);
        LambdaQueryWrapper<ApiLog> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ApiLog::getApiUrl,api);
        wrapper.eq(ApiLog::getDate,nowYMD);
        List<ApiLog> list = this.list(wrapper);
        if (CollUtil.isNotEmpty(list)){
            ApiLog maxApiLog = list.stream().max(Comparator.comparing(ApiLog::getCreateTime)).get();
            return  maxApiLog;
        }
        return null;
    }
}
